package com.onlyxiahui.common.action.description.handler.impl.parameter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.springframework.core.MethodParameter;

import com.onlyxiahui.common.action.description.DocumentContext;
import com.onlyxiahui.common.action.description.bean.MethodData;
import com.onlyxiahui.common.action.description.bean.ModuleData;
import com.onlyxiahui.common.action.description.bean.ParameterData;
import com.onlyxiahui.common.action.description.enums.annotation.ParameterMapping;
import com.onlyxiahui.common.action.description.enums.data.ParameterType;
import com.onlyxiahui.common.action.description.handler.impl.BaseParameterHandler;
import com.onlyxiahui.common.action.description.util.ActionAnnotationUtil;

/**
 * Description <br>
 * Date 2020-01-07 17:24:27<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class DefineParameterHandler extends BaseParameterHandler {

	protected String getAnnotation() {
		return ParameterMapping.Define.getAnnotation();
	}

	protected String getNameKey() {
		return "value";
	}

	@Override
	public boolean support(DocumentContext context, ModuleData moduleData, MethodData methodData, Method method, MethodParameter parameter) {
		Annotation[] as = parameter.getParameterAnnotations();
		boolean has = ActionAnnotationUtil.hasAnnotation(getAnnotation(), as);
		return has;
	}

	@Override
	public ParameterData handle(DocumentContext context, ModuleData moduleData, MethodData methodData, Method method, MethodParameter parameter) {
		String name = null;
		Annotation[] mas = parameter.getParameterAnnotations();
		Object value = ActionAnnotationUtil.getValue(getAnnotation(), getNameKey(), mas);
		if (value instanceof String) {
			name = (String) value;
		}
		ParameterData pd = handle(context, moduleData, methodData, method, parameter, name);
		pd.setParameterType(ParameterType.node.type());
		return pd;
	}
}
